package com.drakulo.hi.logic.helpers;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import com.dk.hi.core.Settings;
import com.drakulo.hi.gfx.InventoryPanelHeroGfx;
import com.drakulo.hi.gfx.Item;
import com.drakulo.hi.logic.io.ImageLoader;
import com.drakulo.hi.logic.io.Resources;
import com.drakulo.hi.states.MenuState;

/**
 * Provides static method to handle mouse
 */
public class MouseHelper {
	private static GameContainer gc;
	private static boolean mouseIsShown;
	private static Image CURSOR;
	private static Image DUMMY_CURSOR;

	/**
	 * Hide the game cursor if is not already done
	 * 
	 * @throws SlickException
	 */
	public static void hideCursor() throws SlickException {
		if (mouseIsShown) {
			mouseIsShown = false;
			gc.setMouseCursor(DUMMY_CURSOR, 0, 0);
		}
	}

	/**
	 * Initializer to call at the very beginning or the game lunch
	 * 
	 * @param gc
	 *            - The GameContainer
	 */
	public static void initialize(GameContainer gc) {
		MouseHelper.gc = gc;
		MouseHelper.mouseIsShown = true;
		try {
			// Here is the classic game cursor
			CURSOR = ImageLoader.load(Resources.CURSOR);
			// Here is a dummy image to fake a hidden cursor
			DUMMY_CURSOR = ImageLoader.load(Resources.DUMMY);

			MouseHelper.gc.setMouseCursor(CURSOR, 0, 0);
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}

	public static boolean mouseIsInEquipmentSquare(int mouseX, int mouseY, int oX, int oY, int deltaX) {
		final int topX = oX + deltaX;
		final int topY = oY;
		final int bottomX = topX + Item.SPRITE_SIZE;
		final int bottomY = topY + Item.SPRITE_SIZE;
		return MouseHelper.mouseIsInRect(mouseX, mouseY, topX, topY, bottomX, bottomY);
	}

	public static boolean mouseIsInEquipmentZone(int mouseX, int mouseY) {
		final int topX = MenuState.MENU_WIDTH;
		final int topY = InventoryPanelHeroGfx.EQUIPMENT_Y;
		final int bottomX = Settings.WINDOW_WIDTH - MenuState.MENU_WIDTH;
		final int bottomY = InventoryPanelHeroGfx.INVENTORY_Y;
		return MouseHelper.mouseIsInRect(mouseX, mouseY, topX, topY, bottomX, bottomY);
	}

	public static boolean mouseIsInInventoryZone(int mouseX, int mouseY) {
		final int topX = MenuState.MENU_WIDTH;
		final int topY = InventoryPanelHeroGfx.INVENTORY_Y;
		final int bottomX = Settings.WINDOW_WIDTH - MenuState.MENU_WIDTH;
		final int bottomY = Settings.WINDOW_HEIGHT;
		return MouseHelper.mouseIsInRect(mouseX, mouseY, topX, topY, bottomX, bottomY);

	}

	/**
	 * Tests if the mouse coordinates are in the rectangle
	 * 
	 * @param mouseX
	 *            - X mouse coordinate
	 * @param mouseY
	 *            - Y mouse coordinate
	 * @param topX
	 *            - Top left X rect coordinate
	 * @param topY
	 *            - Top left Y rect coordinate
	 * @param bottomX
	 *            - Bottom right X rect coordinate
	 * @param bottomY
	 *            - Bottom right Y rect coordinate
	 * @return
	 */
	public static boolean mouseIsInRect(int mouseX, int mouseY, int topX, int topY, int bottomX, int bottomY) {
		if (topX < mouseX && bottomX > mouseX) {
			// X coordinate is OK
			if (topY < mouseY && bottomY > mouseY) {
				// Y coordinate is OK
				return true;
			}
		}
		return false;
	}

	/**
	 * Show the game cursor if is not already done
	 * 
	 * @throws SlickException
	 */
	public static void showCursor() throws SlickException {
		if (!mouseIsShown) {
			gc.setMouseCursor(CURSOR, 0, 0);
			mouseIsShown = true;
		}
	}
}
